﻿Public Class frmQLChungTu
    Private ctrlChungTu As New CChungTuController()
    'các điều kiện lọc
    Public _idDanhMuc As Integer = -1
    Public _idDonVi As Integer = -1
    Public _idMaLoaiNganSach As Integer = -1
    Public _thangQuyetToan As Integer = Date.Now.Month
    Public _namQuyetToan As Integer = Date.Now.Year
    Public _tuNgay As Date = Date.MinValue
    Public _denNgay As Date = Date.MinValue
    Public _soCT As String = ""
    Public _dienGiai As String = ""
    Public _soTienTu As Decimal = Decimal.MinValue
    Public _soTienDen As Decimal = Decimal.MinValue

#Region "Form"
    Private Sub frmQLChungTu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadListView()
    End Sub
    #End Region

#Region "ListView"
    Public Sub LoadListView()
        lvChungTu.BeginUpdate()
        lvChungTu.Items.Clear()
        'lvCTDanhMuc.Columns.Clear()
        'TaoListView()
        'lay ra tat ca cac danh muc con thuoc ma cha do
        Dim listChungTu As IQueryable(Of tblChungTu) = ctrlChungTu.SelectAllByDieuKien(_idDanhMuc, _idDonVi, _idMaLoaiNganSach, _thangQuyetToan, _namQuyetToan, _tuNgay, _denNgay, _soCT, _dienGiai, _soTienTu, _soTienDen)
        For Each ct As tblChungTu In listChungTu
            Dim item As New ListViewItem(ct.NgayChungTu.Value.ToString("dd/MM/yyyy"))
            item.SubItems.Add(ct.SoChungTu)
            item.SubItems.Add(ct.SoTien.Value.ToString("#,##0.##"))
            item.SubItems.Add(ct.NoiDung)
            item.Name = ct.ID_ChungTu
            lvChungTu.Items.Add(item)
        Next
        lvChungTu.EndUpdate()
        If (lvChungTu.Items.Count > 0) Then
            lvChungTu.Items(0).Selected = True
        End If
        lblThongTin.Text = "Tổng cộng có " & lvChungTu.Items.Count & " chứng từ."
    End Sub
   Private Sub lvChungTu_DrawColumnHeader(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) Handles lvChungTu.DrawColumnHeader
        Dim sf As New StringFormat()
        Try
            If (e.ColumnIndex <> 5) Then
                sf.Alignment = StringAlignment.Center
                e.DrawBackground()
                ' Draw the header text.
                Dim headerFont As New Font("Microsoft Sans Serif", 8, FontStyle.Regular)
                e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.Black, e.Bounds, sf)
            End If
        Catch ex As Exception
        Finally
            sf.Dispose()
        End Try
    End Sub
    Private Sub lvChungTu_DrawSubItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs) Handles lvChungTu.DrawSubItem
        Dim brText As Brush = Brushes.Black
        e.DrawBackground()
        Try
            'Neu la Selected Item thi to mau Red, nguoc lai to theo loại phiếu chi hay thu
            If (e.ItemState And DrawItemState.Selected) = DrawItemState.Selected Then
                brText = Brushes.Red
                'Else
                '    Select Case Me.lvChungTu.Items(e.ItemIndex).SubItems(5).Text
                '        Case "True"
                '            brText = Brushes.Blue
                '        Case "False"
                '            brText = Brushes.Black
                '    End Select
            End If
            ' Draw the subitem text in red to highlight it. 
            Dim sf As New StringFormat()
            'Select Case e.ColumnIndex
            '    Case 2 ' số tiền
            '        sf.Alignment = StringAlignment.Far
            '    Case Else
            '        sf.Alignment = StringAlignment.Near
            'End Select
            e.Graphics.DrawString(e.SubItem.Text, Me.lvChungTu.Font, brText, e.Bounds, sf)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
#End Region

#Region "Context Menu"
    Private Sub ChọnTấtCảToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChọnTấtCảToolStripMenuItem.Click
        RefreshDieuKienLoc()
        LoadListView()
    End Sub
    Private Sub LọcChứngTừToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LọcChứngTừToolStripMenuItem.Click
        Dim frm As New frmLocChungTu(Me)
        frm.ShowDialog()
        LoadListView()
    End Sub
    Private Sub XoáChứngTừĐangChọnToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XoáChứngTừĐangChọnToolStripMenuItem.Click
        If (MessageBox.Show("Bạn có chắc muốn xoá tất cả các chứng từ đang chọn?", "Xác nhận", MessageBoxButtons.YesNo) = vbYes) Then
            For Each item As ListViewItem In lvChungTu.SelectedItems
                Dim id As Integer = Integer.Parse(item.Name)
                ctrlChungTu.DeleteRecord(id)
            Next
            LoadListView()
        End If

    End Sub
    Private Sub XuấtChứngTừChọnRaExcelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XuấtChứngTừChọnRaExcelToolStripMenuItem.Click
        'Dim oExcel As Excel.Application
        'Dim oBook As Excel.Workbook
        'Dim oSheet As Excel.Worksheet

        ''Start a new workbook in Excel.
        'oExcel = New Excel.Application
        'oBook = oExcel.Workbooks.Add

        ''Add data to cells of the first worksheet in the new workbook.
        'oSheet = oBook.Worksheets(1)
        ''hiển thị mấy cái tiêu đề
        'oSheet.Range("A1").Value = "DANH SÁCH CÁC CHỨNG TỪ ĐÃ CHỌN THEO YÊU CẦU"
        'oSheet.Range("A2").Value = "Ngày chứng từ"
        'oSheet.Range("B2").Value = "Số chứng từ"
        'oSheet.Range("C2").Value = "Diễn giải"
        'oSheet.Range("D2").Value = "Phát sinh nợ"
        'oSheet.Range("E2").Value = "Phát sinh có"
        'oSheet.Range("F2").Value = "Thuộc loại quỹ"
        'Dim i As Integer = 3
        'For Each item As ListViewItem In lvChungTu.SelectedItems
        '    Dim ct As tblChungTu = ctrlChungTu.SelectRecordByID(Integer.Parse(item.Name))
        '    oSheet.Range(oSheet.Cells(i, 1), oSheet.Cells(i, 1)).Value = ct.NgayChungTu.Value
        '    oSheet.Range(oSheet.Cells(i, 2), oSheet.Cells(i, 2)).Value = ct.SoChungTu
        '    oSheet.Range(oSheet.Cells(i, 3), oSheet.Cells(i, 3)).Value = ct.NoiDung
        '    If (ct.IsNo) Then
        '        oSheet.Range(oSheet.Cells(i, 4), oSheet.Cells(i, 4)).Value = ct.SoTien
        '    Else
        '        oSheet.Range(oSheet.Cells(i, 5), oSheet.Cells(i, 5)).Value = ct.SoTien
        '    End If
        '    oSheet.Range(oSheet.Cells(i, 6), oSheet.Cells(i, 6)).Value = ct.tblLoaiChungTu.TenLoaiCT
        '    i = i + 1
        'Next
        'oExcel.Visible = True
    End Sub
#End Region

#Region "Thanh's Code"
   Public Sub RefreshDieuKienLoc()
        '_tuNgay = Date.MinValue
        '_denNgay = Date.MinValue
        _soCT = ""
        _dienGiai = ""
        _soTienTu = Decimal.MinValue
        _soTienDen = Decimal.MinValue
    End Sub
    Public Sub SetDieuKienLoc(ByVal tuNgay As Date, ByVal denNgay As Date, ByVal danhMucNull As Boolean, ByVal danhMuc As String, ByVal soCT As String, ByVal dienGiai As String, ByVal soTienTu As Decimal, ByVal soTienDen As Decimal)
        _tuNgay = tuNgay
        _denNgay = denNgay
       _soCT = soCT
        _dienGiai = dienGiai
        _soTienTu = soTienTu
        _soTienDen = soTienDen
    End Sub
#End Region

    
    'Private Sub txtDanhMucPath_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs)

    '    Dim curDanhMuc As tblDanhMuc = ctrlDanhMuc.SelectRecordByCode(txtDanhMucPath.Text)
    '    If (curDanhMuc IsNot Nothing) Then
    '        _danhMucChon = curDanhMuc.ID_DanhMuc
    '    Else
    '        _danhMucChon = -1
    '    End If

    'End Sub
End Class